<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
error_reporting(0);
header('Content-Type: text/html; charset=utf-8');

class Admin_product extends CI_Controller {

    public function __construct()
    {
        parent::__construct();
        $this->load->helper('url');
        $this->load->helper('form');
        $this->load->helper('html');
        $this->load->library('session');
        $this->load->model('users_model');
        $this->load->model('admin_product_model');
        $this->load->model('admin_category_model');
        $this->load->model('crud_model');
        $this->load->library('../controllers/crud');
        $this->load->library("pagination");
    }

    public function getTableProductManager($start_from = 0)
    {
        $per_page = 4;
        $config = array();
        $config["base_url"] = base_url() . "/admin_product/getTableProductManager/";
        $config["total_rows"] = $this->admin_product_model->getNumOfTableProductManager();
        $config["per_page"] = $per_page;
        $config["uri_segment"] = 3;
        $config['full_tag_open'] = '<ul>';
        $config['full_tag_close'] = '</ul>';
        $config['first_tag_open'] = '<li>';
        $config['first_tag_close'] = '</li>';
        $config['cur_tag_open'] = '<li class="active">';
        $config['cur_tag_close'] = '</li> ';
        $config['num_tag_open'] = '<li>';
        $config['num_tag_close'] = '</li> ';
        $config['next_link'] = 'Next';
        $config['next_tag_open'] = '<li>';
        $config['next_tag_close'] = '</li>';
        $config['prev_link'] = 'Prev';
        $config['prev_tag_open'] = '<li>';
        $config['prev_tag_close'] = '</li> ';
        $choice = $config["total_rows"] / $config["per_page"];
        $config["num_links"] = round($choice);
        $config['use_page_numbers'] = TRUE;
        $start = $per_page * ($start_from-1);
        if($start < 0){
            $start = 0;
        }
        $data['productList'] = $this->admin_product_model->getTableProductManager($per_page, $start);
        $this->load->view('admin/product/product_list', $data);
    }

    public function getPage()
    {
        $result = $this->admin_product_model->getNumOfTableProductManager();
        $data['num_links'] = $result/4;
        $this->load->view('admin/product/page', $data);
    }

    public function getSubCategory()
    {
        $name = $this->input->post('name');

        $field = 'id';
        $id = $this->product_model->getIDCategory($field, $name);

        $data['subCategory'] = $this->product_model->getSubCategory($id);
        $this->load->view('product/sub_category', $data);
    }

    public function upload()
    {
        $result = $this->crud->multi_upload('product_imagesAdd', 'productFile');
        echo $result;
    }

    public function edit_upload()
    {
        $result = $this->crud->multi_upload('product_imagesEdit', 'productEditFile');
        echo $result;
    }

    public function insertNewProduct(){
        $dt = date("Y-m-d H:i:s");

        $idUser = $this->getIDUser();

        $productName = $this->input->post('productName', true);
        $productCode = $this->input->post('productCode', true);
        $productPrice1 = $this->input->post('productPrice1', true);
        $productPrice2 = $this->input->post('productPrice2', true);
        $productPrice3 = $this->input->post('productPrice3', true);
        $productPrice4 = $this->input->post('productPrice4', true);
        $priceUnit = $this->input->post('priceUnit', true);
        $specifications = $this->input->post('specifications', true);
        $images = $this->session->userdata('product_imagesAdd', true);
        $documents = htmlspecialchars($this->input->post('documents', true));
        $description = htmlspecialchars($this->input->post('description', true));
        $id_sub_category = $this->input->post('subCategoryID', true);
        $active = $this->input->post('active', true);

        $check = $this->admin_product_model->getInfoProductCode($productCode);
        if(!empty($check)){
            echo "Mã sản phẩm đã tồn tại!";
        }
        else{
            $data = array(
                'product_name' => $productName,
                'product_code' => $productCode,
                'product_price1' => $productPrice1,
                'product_price2' => $productPrice2,
                'product_price3' => $productPrice3,
                'product_price4' => $productPrice4,
                'price_unit' => $priceUnit,
                'specifications' => $specifications,
                'images' => $images,
                'documents' => $documents,
                'description' => $description,
                'create_datetime' => $dt,
                'active' => $active,
                'flag' => 0,
                'id_category' => $id_sub_category,
                'id_user' => $idUser
            );
            $query = $this->crud_model->insert("product", $data);
            echo $query;
        }
    }

    public function updateProduct(){
        $dt = date("Y-m-d H:i:s");

        $id = $this->input->post('id', true);
        $productName = $this->input->post('productName', true);

        $productCode = $this->input->post('productCode', true);
        $productCodeCurrent = $this->input->post('productCodeCurrent', true);

        $productPrice1 = $this->input->post('productPrice1', true);
        $productPrice2 = $this->input->post('productPrice2', true);
        $productPrice3 = $this->input->post('productPrice3', true);
        $productPrice4 = $this->input->post('productPrice4', true);
        $priceUnit = $this->input->post('priceUnit', true);
        $specifications = $this->input->post('specifications', true);

        $imagesCurrent = $this->input->post('images', true);
        $imagesUpload = $this->session->userdata('product_imagesEdit', true);
        $images = $imagesCurrent.$imagesUpload;

        $documents = htmlspecialchars($this->input->post('documents', true));
        $description = htmlspecialchars($this->input->post('description', true));
        $id_sub_category = $this->input->post('subCategoryID', true);
        $active = $this->input->post('active', true);

        $data = array(
            'product_name' => $productName,
            'product_code' => $productCode,
            'product_price1' => $productPrice1,
            'product_price2' => $productPrice2,
            'product_price3' => $productPrice3,
            'product_price4' => $productPrice4,
            'price_unit' => $priceUnit,
            'specifications' => $specifications,
            'images' => $images,
            'documents' => $documents,
            'description' => $description,
            'update_datetime' => $dt,
            'active' => $active,
            'id_category' => $id_sub_category,
        );

        if($productCode != $productCodeCurrent){
            $check = $this->admin_product_model->getInfoProductCode($productCode);
            if(!empty($check)){
                echo "Mã sản phẩm đã tồn tại!";
            }
            else{
                $query = $this->crud_model->update('id', $id, "product", $data);
                echo $query;
            }
        }
        else{
            $query = $this->crud_model->update('id', $id, "product", $data);
            echo $query;
        }
    }

    public function deleteProduct(){
        $id = $this->input->post('id');
        $query = $this->admin_product_model->deleteProduct($id);
        echo $query;
    }

    public function searchCategory(){
        $searchString = $this->input->post('searchString');
        $data['searchCategory'] = $this->product_model->searchCategory($searchString);
        //print_r($data);
        $this->load->view('product/search_category', $data);
        //echo $searchString;
    }

    public function getAddParentCategory($id)
    {
//        echo $id;
        $data['info'] = $this->admin_category_model->getParentCategory($id);
        $data['idBefore'] = $id;
        $this->load->view('admin/product/add_category', $data);
    }

    public function getEditParentCategory($id)
    {
//        echo $id;
        $data['info'] = $this->admin_category_model->getParentCategory($id);
        $data['idBefore'] = $id;
        $this->load->view('admin/product/edit_category', $data);
    }

    public function emptyImageAdd(){
        echo $this->crud->emptySession('product_imagesAdd');
    }

    public function emptyImageEdit(){
        echo $this->crud->emptySession('product_imagesEdit');
    }

    public function sessionTest(){
        echo $this->session->userdata('product_imagesAdd');
    }

    public function getIDUser(){
        $email = $this->session->userdata('email');
        $field = 'id';
        $idUser = $this->users_model->getInfoUser($field, $email);
        return $idUser;
    }
}